/** 
 * @file: mlib_key_c.h
 * @author: chen <ischen.x@outlook.com>
 * @brief: This is a key scanning driver library that supports single-click, double-click, 
 *         multiple-click, and long-press events for scanning multiple keys. It is very 
 *         easy to use, consumes minimal resources, and is well-suited for use in embedded
 *         development with this library.
 *         You can define the macro multi_click_enable to enable the detection of multiple-click 
 *         events (enabled by default), or leave it undefined to disable it and reduce resource 
 *         usage. Additionally, you can increase the sensitivity of single-click event scanning.
 * @data: 2024/02/02
 * @version: V1.0
 */
#ifndef __MLIB_KEY_H
#define __MLIB_KEY_H

#include "stdint.h"
#include "stddef.h"

/**
 * @brief: Define this macro to enable multiple stroke events for scanning keysIf you don't need
 *         to scan for multiple-click events, please comment it out.
 */
#define multi_click_enable

/**
 * @brief: Key scanning cycle duration for each scan.
 */
#define key_scan_ticks_ms 				5

/**
 * @brief: Define this macro to increase the sensitivity of key event scanning
 *         Please enter the multiplier for ticks.
 */
#define debouncing_time					25
#define long_press_threshold_ms 		250
#define time_between_multiple_clicks    150

#define mlib_ms_to_ticks(ms)	((int)((ms) / key_scan_ticks_ms))

typedef enum {
	SINGLE_CLICK = 0,
	LONG_PRESS,
#ifdef multi_click_enable	
	DOUBLE_CLICK,
	MULTI_CLICK,
#endif
	number_of_event,
	NONE_PRESS
}BtnEvent;

typedef struct btn Button;
typedef void (*BtnCallback)(Button *);

struct btn
{
    uint8_t id;
    uint8_t event;
    uint32_t last_trigger_ticks;
    uint8_t trigger_status;
	uint8_t trigger_keep_ticks;
#ifdef multi_click_enable
	uint8_t release_ticks;
	uint8_t trigger_times;
#endif
    BtnCallback cb[number_of_event];
	struct btn *next_button;
};

void btn_set_get_gpio_input_fun(uint8_t (*func)(uint8_t));
void btn_set_callback(Button *btn, BtnEvent evt, BtnCallback cbFunc);
void btn_init(Button *btn, uint8_t btn_id, uint8_t btn_active_status);
void button_scan(void);

#endif